layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;

    /**
     * 艺人动态管理--动态列表
     */
    var base = {
        tableId: "baseTable"    //表格id
    };
    
    var bases = {
            tableId: "baseTables"    //表格id
        };

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });
    
    //渲染时间选择框
    laydate.render({
    	elem: '#timeLimits',
    	range: true,
    	max: Feng.currentDate()
    });

    /**
     * 初始化表格的列
     */
    base.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dynamic_content', align: "center", sort: true, title: '标题'},
            {field: 'dynamic_image', align: "center", sort: true, title: '内容'},
            {field: 'dzSl', align: "center", sort: true, title: '点赞数量'},
            {field: 'scSl', align: "center", sort: true, title: '收藏数量'},
            {field: 'zfSl', align: "center", sort: true, title: '分享数量'},
            {field: 'nickName', align: "center", sort: true, title: '发布用户'},
            {field: 'create_time', align: "center", sort: true, title: '发布时间'},
            {field: 'index_show', align: "center", sort: true, title: '精选',templet:function(d) {
            	if(d.index_show == 1){
            		return '<p class="layui-btn layui-btn-yes-b layui-btn-xs">精选</p>';
            	}else if(d.index_show == 0){
            		return '-';
            	}
            }},
            {field: 'is_audit', align: "center", sort: true, title: '状态',templet:function(d) {
            	if(d.is_audit == 1){
            		return '<p class="layui-btn layui-btn-yes-b layui-btn-xs">已通过</p>';
            	}else if(d.is_audit == 2){
            		return '<p class="layui-btn layui-btn-yes-a layui-btn-xs">已拒绝</p>';
            	}else if(d.is_audit == 0){
            		return '<p class="layui-btn layui-btn-yes-a layui-btn-xs">待审核</p>';
            	}
            }},
            {fixed: 'right', toolbar: '#toolBtn', title: '操作', width: 350, align: 'center' }
        ]]
    };
    
    bases.initColumn = function () {
        return [[
                 {type: 'checkbox'},
                 {field: 'dynamic_content', align: "center", sort: true, title: '标题'},
                 {field: 'dynamic_image', align: "center", sort: true, title: '内容'},
                 {field: 'dzSl', align: "center", sort: true, title: '点赞数量'},
                 {field: 'scSl', align: "center", sort: true, title: '收藏数量'},
                 {field: 'zfSl', align: "center", sort: true, title: '分享数量'},
                 {field: 'nickName', align: "center", sort: true, title: '发布用户'},
                 {field: 'create_time', align: "center", sort: true, title: '发布时间'},
                 {field: 'is_audit', align: "center", sort: true, title: '状态',templet:function(d) {
                 	if(d.is_audit == 1){
                 		return '<p class="layui-btn layui-btn-yes-b layui-btn-xs">已通过</p>';
                 	}else if(d.is_audit == 2){
                 		return '<p class="layui-btn layui-btn-yes-a layui-btn-xs">已拒绝</p>';
                 	}else if(d.is_audit == 0){
                 		return '<p class="layui-btn layui-btn-yes-a layui-btn-xs">待审核</p>';
                 	}
                 }},
                 {fixed: 'right', toolbar: '#toolBtns', title: '操作', width: 350, align: 'center' }
                ]]
    }
    
    //绑定事件
    $(document).on('click', '#adopta', function(data) {
        var id = $(this).attr("data-id");
        var type = $(this).attr("data-name");
        var pushId = $(this).attr("data-class");
        var status = $(this).attr("name");
        var title = "";
        if(status == '1'){
    		title = "你确认要设置精选吗?";
    	}else{
    		title = "你确认要取消精选吗?";
    	}
		layer.confirm(title, function() {
			var data = {
					id : id,
					pushId : pushId,
					indexShow : status
				};
			//调用ajax局部启动
			$.ajax({
				url : Feng.ctxPath + "/modular/user_dynamic/update",
				type : "POST",
				data :JSON.stringify(data),
				dataType : "json",
				contentType: "application/json;charset=UTF-8",
				async : false,
				cache : false,
				success : function(obj) {
					if (obj != null) {
						if (obj.success) {
							layer.msg("操作成功");
						} else {
							layer.msg("操作失败");
						}
					}
				}
			});
			if(type == "1"){
				tableResult.reload();
			}else{
				tableResults.reload();
			}
			setTimeout("layer.close(layer.index);",2000);
		});
    });
    
    //绑定事件
    $(document).on('click', '#adoptb', function(data) {
        var id = $(this).attr("data-id");
        var type = $(this).attr("data-name");
        var status = $(this).attr("name");
        var title = "";
        if(status == '1'){
    		title = "你确认要审核通过吗?";
    	}else{
    		title = "你确认要审核拒绝吗?";
    	}
		layer.confirm(title, function() {
			var data = {
					id : id,
					isAudit : status
				};
			//调用ajax局部启动
			$.ajax({
				url : Feng.ctxPath + "/modular/user_dynamic/update",
				type : "POST",
				data :JSON.stringify(data),
				dataType : "json",
				contentType: "application/json;charset=UTF-8",
				async : false,
				cache : false,
				success : function(obj) {
					if (obj != null) {
						if (obj.success) {
							layer.msg("操作成功");
						} else {
							layer.msg("操作失败");
						}
					}
				}
			});
			if(type == "1"){
				tableResult.reload();
			}else{
				tableResults.reload();
			}
			setTimeout("layer.close(layer.index);",2000);
		});
    });
    
    $(document).on('click', '#adopt', function(data) {
        var id = $(this).attr("data-id");
		 func.open({
	            type: 2,
	            shadeClose: true,
	            shade: 0.5,
	            area: '300px',
	            title: '查看',
	            maxmin: true, //开启最大化最小化按钮
	            content: Feng.ctxPath + "/modular/user_dynamic/details/" + id,
	            tableId: base.tableId
	        });
    });
    
    $(document).on('click', '#adoptrm', function(data) {
        var id = $(this).attr("data-id");
        var type = $(this).attr("data-name");
		layer.confirm('你确认要删除此条动态吗?', function() {
			//调用ajax局部启动
			$.ajax({
				url : Feng.ctxPath + "/modular/user_dynamic/delete?id="+id,
				type : "POST",
				dataType : "json",
				async : false,
				cache : false,
				success : function(obj) {
					if (obj != null) {
						if (obj.success) {
							layer.msg("操作成功");
						} else {
							layer.msg("操作失败");
						}
					}
				}
			});
			if(type == "1"){
				tableResult.reload();
			}else{
				tableResults.reload();
			}
			setTimeout("layer.close(layer.index);",2000);
		});
    });
    
    $(document).on('click', '#imgck', function(data) {
    	var imgHtml = "<img src='" + $(this).attr("src") + "' />";  
    	layer.open({ 
    		type: 1, 
    		shade: false,
    		title: false,
    		area: [790+'px', 500+'px'],
    		content: imgHtml,
    		cancel: function () {
    			
    		}
	    });
    });
    
    //绑定事件
    $(document).on('click', '#btn', function(data) {
    	$(this).addClass("layui-btn-group-yans").removeClass("layui-btn-group-nans");
    	$("#btns").addClass("layui-btn-group-nans").removeClass("layui-btn-group-yans");
    	$("#diva").show();
    	$("#divb").hide();
    	$("#taba").show();
    	$("#tabb").hide();
    	tableResult.reload();
    });
    
    //绑定事件
    $(document).on('click', '#btns', function(data) {
    	$(this).addClass("layui-btn-group-yans").removeClass("layui-btn-group-nans");
    	$("#btn").addClass("layui-btn-group-nans").removeClass("layui-btn-group-yans");
    	$("#divb").show();
    	$("#diva").hide();
    	$("#tabb").show();
    	$("#taba").hide();
    	 // 渲染表格
    	tableResults.reload();
    });
    
    // 渲染表格
    var tableResults = table.render({
        title: "精选列表",
        elem: '#' + bases.tableId,
        url: Feng.ctxPath + '/modular/user_dynamic/lists',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: bases.initColumn()
    });



    /**
     * 点击查询按钮
     */
    base.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#keyword").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['indexShow'] = $("#status").find("option:selected").val();
        queryData['isAudit'] = $("#isAudit").find("option:selected").val();
        table.reload(base.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    
    /**
     * 点击查询按钮
     */
    bases.searchs = function () {
        var queryData = {};
        queryData['keyword'] = $("#keywords").val();
        queryData['timeLimit'] = $("#timeLimits").val();
        queryData['isAudit'] = $("#isAudits").find("option:selected").val();
        table.reload(bases.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        base.search();
    });
    
    // 搜索按钮点击事件
    $('#btnSearchs').click(function () {
        bases.searchs();
    });

    // 导出excel
    $('#btnExp').click(function () {
        base.exportExcel();
    });
    
    // 导出excel
    $('#btnExps').click(function () {
    	bases.exportExcels();
    });
    
    // 渲染表格
    var tableResult = table.render({
        title: "全部列表",
        elem: '#' + base.tableId,
        url: Feng.ctxPath + '/modular/user_dynamic/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: base.initColumn()
    });
    
    base.exportExcel = function () {
        var checkRows = table.checkStatus(base.tableId);

        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            // table.exportFile([' 表头',' 表头',' 表头',' 表头',' 表头',' 表头'], cons , 'xls',name);
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    
    bases.exportExcels = function () {
        var checkRows = table.checkStatus(bases.tableId);

        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            // table.exportFile([' 表头',' 表头',' 表头',' 表头',' 表头',' 表头'], cons , 'xls',name);
            table.exportFile(tableResults.config.id, checkRows.data, 'xls');
        }
    };
});